//Poperty of MATTHEW M. TONDREAU
//SNAKE Version 1.0 Beta
//2009 GNU General Public License v3
//http://code.google.com/p/win32snake

#include "gamelib.h"

int main()
{
	char again;
	srand(unsigned(time(0)) );
	Title::title();
	unsigned STARTING_SNAKE_SIZE = Game_Settings::SNAKE_SIZE;
	
	do
	{
		game_reset();
		Food food(WIDTH_MIN + 2,HEIGHT_MIN + 2);
		//food.Change();
		unsigned food_eaten = 0;
		

		Snake mysnake(STARTING_SNAKE_POS_X,STARTING_SNAKE_POS_Y,Snake::LEFT);
		for(unsigned i = 0; i < STARTING_SNAKE_SIZE; i++)
		{
			mysnake.AddSegment();
		}

		Setup(food,mysnake);

		unsigned int idThread; //id of thread
		unsigned long hThread = _beginthreadex(NULL,THREAD_SUSPEND_RESUME,MainLopp,&mysnake, 0,&idThread);
		if(hThread) //if thread created succesfully
		{
			
			ResumeThread(HANDLE(hThread));
		}
		else 
		{
			cout << "Thread creation fail\n";
			return 1;	
		}


		
		do //ENTER GAME LOOP
		{
			WaitForSingleObject(Game_Settings::hMutex,INFINITE);
			mysnake.Update();
			mysnake.Draw();
			
			if(mysnake.CheckForColision())
			{
				ixion::SetCursorPos(GAME_OVER_X,GAME_OVER_Y);
				cout << "GAME OVER";
				Game_Settings::FINISH = true;
			}
			if(CheckForFood(mysnake.Head(),food,mysnake))
			{
				UpdateDifficultyCheck(food_eaten);
			}
			food.Draw();
			ReleaseMutex(Game_Settings::hMutex);
			Sleep(Game_Settings::GAME_SPEED);


		}while(!Game_Settings::FINISH);
		//END GAME LOOP
		CheckHighScores(Game_Settings::player_score);

		ixion::SetCursorPos(PLAY_AGAIN_X,PLAY_AGAIN_Y);
		cout << "PLAY AGAIN(Y/N)?";
		cin >> again;
	}while( (again | 32) == 'y' ); 

	ixion::SetCursorPos(THANK_YOU_X,THANK_YOU_Y);
	cout << "THANK YOU FOR PLAYING";
	ixion::SetCursorPos(END_CURSOR_X,END_CURSOR_Y);



}
